package com.example.core.hql;

import com.example.utils.StringUtils;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class HQLWrapper {
    public HQLInfo info;

    public String hql;
    public List<HQLParameter> parameterList = new ArrayList<HQLParameter>();

    public HQLWrapper(HQLInfo info) {
        this.info = info;
        this.parameterList = this.info.getParameterList();
        StringBuffer stringBuffer = new StringBuffer();
        String modelName = this.info.getModelName();
        String modelTable = StringUtils.toLowerCaseFirstOne(modelName);
        if (this.info.isGetCount()) {
            stringBuffer.append("select count(*) ");
        } else {
            if (!StringUtils.isEmpty(this.info.getSelectBlock())) {
                stringBuffer.append("select " + this.info.getSelectBlock() + " ");
            }
        }
        stringBuffer.append(" from " + modelName + " " + modelTable + " ");
        if (!StringUtils.isEmpty(this.info.getJoinBlock())) {
            stringBuffer.append(this.info.getJoinBlock() + " ");
        }
        if (!StringUtils.isEmpty(this.info.getWhereBlock())) {
            stringBuffer.append(" where " + this.info.getWhereBlock());
        }
        if (!this.info.isGetCount()) {
            if (!StringUtils.isEmpty(this.info.getOrderBy())) {
                stringBuffer.append(" order by " + this.info.getOrderBy());
            }
        }
        this.hql = stringBuffer.toString();
    }
}
